package com.rm.freedrawview;

import ohos.agp.render.Paint;
import ohos.agp.render.Path;

import ohos.utils.Parcel;
import ohos.utils.Sequenceable;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Created by Riccardo Moro on 9/27/2016.
 *
 * @author a
 * @version 1.0.0
 */
public class HistoryPath implements Sequenceable, Serializable {
    static final long serialVersionUID = 41L;

    private static final String TAG = HistoryPath.class.getSimpleName();

    private ArrayList<Point> points = new ArrayList<>();
    private int paintColor;
    private int paintAlpha;
    private float paintWidth;
    private float originX, originY;
    private boolean isPoint;

    private transient Path path = null;
    private transient Paint paint = null;

    HistoryPath(ArrayList<Point> points, Paint paint) {
        this.points = new ArrayList<>(points);
        this.paintColor = paint.getColor().getValue();
        this.paintAlpha = (int) (paint.getAlpha() * 255);
        this.paintWidth = paint.getStrokeWidth();
        this.originX = points.get(0).x;
        this.originY = points.get(0).y;
        this.isPoint = FreeDrawHelper.isAPoint(points);
        generatePath();
        generatePaint();
    }

    /**
     * generatePath.
     */
    public void generatePath() {
        path = new Path();
        if (points != null) {
            boolean first = true;
            for (int i = 0; i < points.size(); i++) {
                Point point = points.get(i);
                if (first) {
                    path.moveTo(point.x, point.y);
                    first = false;
                } else {
                    path.lineTo(point.x, point.y);
                }
            }
        }
    }

    private void generatePaint() {
        paint = FreeDrawHelper.createPaintAndInitialize(paintColor, paintAlpha, paintWidth,
                isPoint);
    }

    /**
     * getPath.
     *
     * @return Path
     */
    public Path getPath() {
        if (path == null) {
            generatePath();
        }
        return path;
    }

    /**
     * isPoint .
     *
     * @return boolean
     */
    public boolean isPoint() {
        return isPoint;
    }

    /**
     * setPoint .
     *
     * @param point point
     */
    public void setPoint(boolean point) {
        isPoint = point;
    }

    /**
     * getOriginX .
     *
     * @return float
     */
    public float getOriginX() {
        return originX;
    }

    /**
     * setOriginX .
     *
     * @param originX originX
     */
    public void setOriginX(float originX) {
        this.originX = originX;
    }

    /**
     * getOriginY .
     *
     * @return float
     */
    public float getOriginY() {
        return originY;
    }

    /**
     * setOriginY .
     *
     * @param originY originY
     */
    public void setOriginY(float originY) {
        this.originY = originY;
    }

    /**
     * getPaintColor .
     *
     * @return int
     */
    public int getPaintColor() {
        return paintColor;
    }

    /**
     * setPaintColor .
     *
     * @param paintColor paintColor
     */
    public void setPaintColor(int paintColor) {
        this.paintColor = paintColor;
    }

    /**
     * getPaintAlpha.
     *
     * @return int
     */
    public int getPaintAlpha() {
        return paintAlpha;
    }

    /**
     * setPaintAlpha.
     *
     * @param paintAlpha paintAlpha
     */
    public void setPaintAlpha(int paintAlpha) {
        this.paintAlpha = paintAlpha;
    }

    /**
     * getPaintWidth .
     *
     * @return float
     */
    public float getPaintWidth() {
        return paintWidth;
    }

    /**
     * setPaintWidth .
     *
     * @param paintWidth paintWidth
     */
    public void setPaintWidth(float paintWidth) {
        this.paintWidth = paintWidth;
    }

    /**
     * getPaint .
     *
     * @return Paint
     */
    public Paint getPaint() {
        if (paint == null) {
            generatePaint();
        }
        return paint;
    }

    /**
     * getPoints .
     *
     * @return ArrayList
     */
    public ArrayList<Point> getPoints() {
        return points;
    }

    /**
     * setPoints .
     *
     * @param points points
     */
    public void setPoints(ArrayList<Point> points) {
        this.points = points;
    }

    @Override
    public boolean marshalling(Parcel dest) {
        dest.writeSequenceableList(points);
        dest.writeInt(paintColor);
        dest.writeInt(paintAlpha);
        dest.writeFloat(paintWidth);
        dest.writeFloat(originX);
        dest.writeFloat(originY);
        dest.writeByte((byte) (isPoint ? 1 : 0));
        return true;
    }

    @Override
    public boolean unmarshalling(Parcel in) {
        points = (ArrayList<Point>) in.readSequenceableList(Point.class);
        paintColor = in.readInt();
        paintAlpha = in.readInt();
        paintWidth = in.readFloat();
        originX = in.readFloat();
        originY = in.readFloat();
        isPoint = in.readByte() != 0;
        generatePath();
        generatePaint();
        return true;
    }
}
